using HKB.Common;
using HKB.Data.Model;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace HKB.Data.Dal
{
	public class CouponInfo
	{
		public int GetMaxId()
		{
			return Conn.GetMaxID("CouponInfoID", "HL_CouponInfo");
		}
		public bool Exists(int CouponInfoID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) from HL_CouponInfo");
			stringBuilder.Append(" where CouponInfoID=@CouponInfoID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CouponInfoID", SqlDbType.Int, 4)
			};
			array[0].Value = CouponInfoID;
			return Conn.Exists(stringBuilder.ToString(), array);
		}
		public int Add(HKB.Data.Model.CouponInfo model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("insert into HL_CouponInfo(");
			stringBuilder.Append("CouponName,CouponNumPattern,CouponParvalue,NeedsMoney,BeginDate,EndDate,UserGroupID,GetFashion,GetFashionContent,UseProductType,UseProductIDs,CouponCount,CouponIsPublish)");
			stringBuilder.Append(" values (");
			stringBuilder.Append("@CouponName,@CouponNumPattern,@CouponParvalue,@NeedsMoney,@BeginDate,@EndDate,@UserGroupID,@GetFashion,@GetFashionContent,@UseProductType,@UseProductIDs,@CouponCount,@CouponIsPublish)");
			stringBuilder.Append(";select @@IDENTITY");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CouponName", SqlDbType.NVarChar, 30),
				new SqlParameter("@CouponNumPattern", SqlDbType.NVarChar, 30),
				new SqlParameter("@CouponParvalue", SqlDbType.Decimal, 5),
				new SqlParameter("@NeedsMoney", SqlDbType.Decimal, 5),
				new SqlParameter("@BeginDate", SqlDbType.DateTime),
				new SqlParameter("@EndDate", SqlDbType.DateTime),
				new SqlParameter("@UserGroupID", SqlDbType.NVarChar, 30),
				new SqlParameter("@GetFashion", SqlDbType.NVarChar, 30),
				new SqlParameter("@GetFashionContent", SqlDbType.NVarChar, 255),
				new SqlParameter("@UseProductType", SqlDbType.NVarChar, 30),
				new SqlParameter("@UseProductIDs", SqlDbType.NVarChar, 255),
				new SqlParameter("@CouponCount", SqlDbType.Int, 4),
				new SqlParameter("@CouponIsPublish", SqlDbType.Bit, 1)
			};
			array[0].Value = model.CouponName;
			array[1].Value = model.CouponNumPattern;
			array[2].Value = model.CouponParvalue;
			array[3].Value = model.NeedsMoney;
			array[4].Value = model.BeginDate;
			array[5].Value = model.EndDate;
			array[6].Value = model.UserGroupID;
			array[7].Value = model.GetFashion;
			array[8].Value = model.GetFashionContent;
			array[9].Value = model.UseProductType;
			array[10].Value = model.UseProductIDs;
			array[11].Value = model.CouponCount;
			array[12].Value = model.CouponIsPublish;
			object single = Conn.GetSingle(stringBuilder.ToString(), array);
			int result;
			if (single == null)
			{
				result = 0;
			}
			else
			{
				result = System.Convert.ToInt32(single);
			}
			return result;
		}
		public bool Update(HKB.Data.Model.CouponInfo model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("update HL_CouponInfo set ");
			stringBuilder.Append("CouponName=@CouponName,");
			stringBuilder.Append("CouponNumPattern=@CouponNumPattern,");
			stringBuilder.Append("CouponParvalue=@CouponParvalue,");
			stringBuilder.Append("NeedsMoney=@NeedsMoney,");
			stringBuilder.Append("BeginDate=@BeginDate,");
			stringBuilder.Append("EndDate=@EndDate,");
			stringBuilder.Append("UserGroupID=@UserGroupID,");
			stringBuilder.Append("GetFashion=@GetFashion,");
			stringBuilder.Append("GetFashionContent=@GetFashionContent,");
			stringBuilder.Append("UseProductType=@UseProductType,");
			stringBuilder.Append("UseProductIDs=@UseProductIDs,");
			stringBuilder.Append("CouponCount=@CouponCount,");
			stringBuilder.Append("CouponIsPublish=@CouponIsPublish");
			stringBuilder.Append(" where CouponInfoID=@CouponInfoID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CouponName", SqlDbType.NVarChar, 30),
				new SqlParameter("@CouponNumPattern", SqlDbType.NVarChar, 30),
				new SqlParameter("@CouponParvalue", SqlDbType.Decimal, 5),
				new SqlParameter("@NeedsMoney", SqlDbType.Decimal, 5),
				new SqlParameter("@BeginDate", SqlDbType.DateTime),
				new SqlParameter("@EndDate", SqlDbType.DateTime),
				new SqlParameter("@UserGroupID", SqlDbType.NVarChar, 30),
				new SqlParameter("@GetFashion", SqlDbType.NVarChar, 30),
				new SqlParameter("@GetFashionContent", SqlDbType.NVarChar, 255),
				new SqlParameter("@UseProductType", SqlDbType.NVarChar, 30),
				new SqlParameter("@UseProductIDs", SqlDbType.NVarChar, 255),
				new SqlParameter("@CouponCount", SqlDbType.Int, 4),
				new SqlParameter("@CouponIsPublish", SqlDbType.Bit, 1),
				new SqlParameter("@CouponInfoID", SqlDbType.Int, 4)
			};
			array[0].Value = model.CouponName;
			array[1].Value = model.CouponNumPattern;
			array[2].Value = model.CouponParvalue;
			array[3].Value = model.NeedsMoney;
			array[4].Value = model.BeginDate;
			array[5].Value = model.EndDate;
			array[6].Value = model.UserGroupID;
			array[7].Value = model.GetFashion;
			array[8].Value = model.GetFashionContent;
			array[9].Value = model.UseProductType;
			array[10].Value = model.UseProductIDs;
			array[11].Value = model.CouponCount;
			array[12].Value = model.CouponIsPublish;
			array[13].Value = model.CouponInfoID;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool Delete(int CouponInfoID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from HL_CouponInfo ");
			stringBuilder.Append(" where CouponInfoID=@CouponInfoID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CouponInfoID", SqlDbType.Int, 4)
			};
			array[0].Value = CouponInfoID;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool DeleteList(string CouponInfoIDlist)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from HL_CouponInfo ");
			stringBuilder.Append(" where CouponInfoID in (" + CouponInfoIDlist + ")  ");
			int num = Conn.ExecuteSql(stringBuilder.ToString(), new IDbDataParameter[0]);
			return num > 0;
		}
		public HKB.Data.Model.CouponInfo GetModel(int CouponInfoID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select  top 1 * from HL_CouponInfo ");
			stringBuilder.Append(" where CouponInfoID=@CouponInfoID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CouponInfoID", SqlDbType.Int, 4)
			};
			array[0].Value = CouponInfoID;
			HKB.Data.Model.CouponInfo couponInfo = new HKB.Data.Model.CouponInfo();
			DataSet dataSet = Conn.Query(stringBuilder.ToString(), array);
			HKB.Data.Model.CouponInfo result;
			if (dataSet.Tables[0].Rows.Count > 0)
			{
				result = this.DataRowToModel(dataSet.Tables[0].Rows[0]);
			}
			else
			{
				result = null;
			}
			return result;
		}
		public HKB.Data.Model.CouponInfo DataRowToModel(DataRow row)
		{
			HKB.Data.Model.CouponInfo couponInfo = new HKB.Data.Model.CouponInfo();
			if (row != null)
			{
				if (row["CouponInfoID"] != null && row["CouponInfoID"].ToString() != "")
				{
					couponInfo.CouponInfoID = int.Parse(row["CouponInfoID"].ToString());
				}
				if (row["CouponName"] != null)
				{
					couponInfo.CouponName = row["CouponName"].ToString();
				}
				if (row["CouponNumPattern"] != null)
				{
					couponInfo.CouponNumPattern = row["CouponNumPattern"].ToString();
				}
				if (row["CouponParvalue"] != null && row["CouponParvalue"].ToString() != "")
				{
					couponInfo.CouponParvalue = new decimal?(decimal.Parse(row["CouponParvalue"].ToString()));
				}
				if (row["NeedsMoney"] != null && row["NeedsMoney"].ToString() != "")
				{
					couponInfo.NeedsMoney = new decimal?(decimal.Parse(row["NeedsMoney"].ToString()));
				}
				if (row["BeginDate"] != null && row["BeginDate"].ToString() != "")
				{
					couponInfo.BeginDate = new System.DateTime?(System.DateTime.Parse(row["BeginDate"].ToString()));
				}
				if (row["EndDate"] != null && row["EndDate"].ToString() != "")
				{
					couponInfo.EndDate = new System.DateTime?(System.DateTime.Parse(row["EndDate"].ToString()));
				}
				if (row["UserGroupID"] != null && row["UserGroupID"].ToString() != "")
				{
					couponInfo.UserGroupID = row["UserGroupID"].ToString();
				}
				if (row["GetFashion"] != null)
				{
					couponInfo.GetFashion = row["GetFashion"].ToString();
				}
				if (row["GetFashionContent"] != null)
				{
					couponInfo.GetFashionContent = row["GetFashionContent"].ToString();
				}
				if (row["UseProductType"] != null)
				{
					couponInfo.UseProductType = row["UseProductType"].ToString();
				}
				if (row["UseProductIDs"] != null)
				{
					couponInfo.UseProductIDs = row["UseProductIDs"].ToString();
				}
				if (row["CouponCount"] != null && row["CouponCount"].ToString() != "")
				{
					couponInfo.CouponCount = new int?(int.Parse(row["CouponCount"].ToString()));
				}
				if (row["CouponIsPublish"] != null && row["CouponIsPublish"].ToString() != "")
				{
					if (row["CouponIsPublish"].ToString() == "1" || row["CouponIsPublish"].ToString().ToLower() == "true")
					{
						couponInfo.CouponIsPublish = true;
					}
					else
					{
						couponInfo.CouponIsPublish = false;
					}
				}
			}
			return couponInfo;
		}
		public DataSet GetList(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select * FROM HL_CouponInfo ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public DataSet GetList(int Top, string strWhere, string filedOrder)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select ");
			if (Top > 0)
			{
				stringBuilder.Append(" top " + Top.ToString());
			}
			stringBuilder.Append(" * FROM HL_CouponInfo ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			stringBuilder.Append(" order by " + filedOrder);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public int GetRecordCount(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) FROM HL_CouponInfo ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			object single = DbHelperSQL.GetSingle(stringBuilder.ToString());
			int result;
			if (single == null)
			{
				result = 0;
			}
			else
			{
				result = System.Convert.ToInt32(single);
			}
			return result;
		}
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("SELECT * FROM ( ");
			stringBuilder.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				stringBuilder.Append("order by T." + orderby);
			}
			else
			{
				stringBuilder.Append("order by T.CouponInfoID desc");
			}
			stringBuilder.Append(")AS Row, T.*  from HL_CouponInfo T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				stringBuilder.Append(" WHERE " + strWhere);
			}
			stringBuilder.Append(" ) TT");
			stringBuilder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
	}
}
